#include "headfiles.h"

using namespace std;

int main(int argc, char **argv) {
    if (argc != 2) {
        cout << "Usage: " << argv[0] << " file" << endl;
        return 0;
    }

    fstream fs;
    fs.open(argv[1], iostream::in);
    if (!fs.is_open()) {
        cout << "File " << argv[1] << " open error!" << endl;
        return 1;
    }

    vector<int> vec;
    string str;
    while (fs >> str) {
        if (str.empty()) {
            break;
        }

        int val = atoi(str.c_str());
        vec.push_back(val);
    }
    fs.close();

    int size = vec.size();
    int *a = (int *)malloc(sizeof(int) * size);
    for (int i = 0; i < size; i++) {
        a[i] = vec[i];
    }

    int value = trap(a, size);

    cout << "Water amount = " << value << endl;

    free(a);

    return 0;
}

